package medium删除结点;

import org.testng.annotations.Test;

public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode rail = head;
        ListNode list = rail;
        int i = 0;
        while(head != null){
            i++;
            head = head.next;
        }
        if (i == 1 && n==1)
            return head;
        if (i-n-1<0){
            list = list.next;
            return list;
        }
        for (int j = 0; j < i-n-1; j++)
            rail = rail.next;
        rail.next = rail.next.next;
        return list;
    }
    @Test
    public void test(){
        ListNode l1 = new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,
                new ListNode(5,null)))));
//        ListNode l1 = new ListNode(1,new ListNode(2,null));
        ListNode T1 = removeNthFromEnd(l1,1);
        while(T1 != null){
            System.out.println(T1.val);
            T1 = T1.next;
        }
    }
}
